﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;

namespace BigMailer.Services
{
	public class CustomHostFactory : ServiceHostFactory
	{
		protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
		{
			string inboundUrl = HttpContext.Current.Request.Url.AbsoluteUri;
			int index = inboundUrl.LastIndexOf( '/' );
			string baseUrl = inboundUrl.Substring( 0, index + 1 ).ToLower();

			Uri theUri = baseAddresses[0];
			foreach ( Uri uri in baseAddresses )
			{
				if (uri.AbsoluteUri.ToLower().StartsWith(baseUrl))
				{
					theUri = uri;
				}
			}

			return base.CreateServiceHost( serviceType, 
				new Uri[] { theUri } );
		}
	}
}